package com.xiaohudie.JDBC;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.UUID;

/**
 * @Author xiao hu die
 * @Date 2022/3/13 12:45
 * @Version 1.0
 */
/**java.sql.Statement的特点就是先拼接在编译
 * 也是出现sql注入的根本原因
 * java.sql.PreparedStatement的特点就是先进行sql语句的编译，再给sql语句传值*/
public class TestJdbcPiLiang {
    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
        Properties properties =new Properties();
        properties.load(new FileReader("src//dp.properties"));
        Connection connection =null;
        PreparedStatement statement =null;
        ResultSet resultSet =null;
        String url = properties.getProperty("url");
        String root = properties.getProperty("user");
        String password =properties.getProperty("password");
        String driver =properties.getProperty("driver");
        /**注册驱动*/

       try {
           Class.forName(driver);

           /**获取连接*/
           connection = DriverManager.getConnection(url, root, password);
           /**获取执行sql的对象*/
           /**获取sql语句*/
           String InsertSql = "INSERT INTO student (id,name,email,age) VALUES (?,?,?,?)";
           statement = connection.prepareStatement(InsertSql);
           for (int i = 1; i <= 50; i++) {
               statement.setInt(1, i);
               statement.setString(2, UUID.randomUUID().toString().substring(1, 6));
               statement.setString(3, "1357494913@qq.com");
               statement.setInt(4,i);
               /**执行sql语句*/
               int hangshu = statement.executeUpdate();
               System.out.println("受影响的行数---》" + hangshu);
           }
       }finally {
           if (resultSet!=null){
               try {
                   resultSet.close();
               } catch (SQLException e) {
                   e.printStackTrace();
               }
           }
           if (statement!=null){
               try {
                   statement.close();
               } catch (SQLException e) {
                   e.printStackTrace();
               }
           }
           if (connection!=null){
               try {
                   connection.close();
               } catch (SQLException e) {
                   e.printStackTrace();
               }
           }
       }
       }

}
